import 'package:cloud_medicine/components/shop_picture.dart';
import 'package:cloud_medicine/pages/medicine_detail.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class CartItem extends StatefulWidget {
  num medicineID;
  final String name;
  final String imgUrl;
  num price;
  num medicineNumber;
  final ValueChanged addcallback;
  final ValueChanged removecallback;
  CartItem(
      {Key key,
      this.imgUrl,
      this.name,
      this.price,
      this.medicineNumber = 1,
      this.addcallback,
      this.removecallback,
      this.medicineID})
      : super(key: key);
  @override
  _CartItemState createState() => _CartItemState();
}

class _CartItemState extends State<CartItem> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: <Widget>[
            Divider(
              thickness: 2,
            ),
            GestureDetector(
              onTap: () {
                Navigator.of(context).push(MaterialPageRoute(
                    builder: (context) => MedicineDetail(
                          medicineId: widget.medicineID,
                        )));
              },
              child: Row(
                children: <Widget>[
                  Container(
                    child: ShopImg(
                      imgUrl: widget.imgUrl,
                      imageStyle: 60,
                    ),
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.only(left: 10),
                        // color: Colors.black,
                        width: MediaQuery.of(context).size.width - 100,
                        child: Text(
                          '${widget.name}',
                          style: TextStyle(fontSize: 22),
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.only(left: 10),
                            child: Text(
                              '￥${(widget.price * widget.medicineNumber).toStringAsFixed(2)}',
                              style: TextStyle(color: Colors.red),
                            ),
                          ),
                          SizedBox(
                            width: 100,
                          ),
                          Container(
                            child: Row(
                              children: <Widget>[
                                IconButton(
                                    padding: EdgeInsets.all(0),
                                    icon: Icon(Icons.remove_circle_outline),
                                    onPressed: () {
                                      setState(() {
                                        widget.medicineNumber--;
                                        widget
                                            .removecallback(widget.medicineID);
                                      });
                                    }),
                                Text('${widget.medicineNumber}'),
                                IconButton(
                                    padding: EdgeInsets.all(0),
                                    color: Colors.yellowAccent[700],
                                    icon: Icon(Icons.add_circle),
                                    onPressed: () {
                                      setState(() {
                                        widget.medicineNumber++;
                                        widget.addcallback(widget.medicineID);
                                      });
                                    }),
                              ],
                            ),
                          )
                        ],
                      )
                    ],
                  )
                ],
              ),
            )
          ],
        ));
  }
}

class CartListText extends StatelessWidget {
  final String text;
  final double size;
  const CartListText({Key key, this.text, this.size = 10}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(
      text,
      style: TextStyle(color: Colors.white, fontSize: size),
    );
  }
}
